<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      body,
      #container {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
        position: absolute;
      }
    </style>
    <script type="importmap">
      {
        "imports": {
          "three": "../../node_modules/three/build/three.module.js",
          "three/addons/": "../../node_modules/three/examples/jsm/",
          "tween": "../../node_modules/three/examples/jsm/libs/tween.module.js"
        }
      }
    </script>
    <script src="https://webapi.amap.com/maps?v=2.0&key=d994a78081a6847648e3faa54be6b74a"></script>
  </head>
  <body>
    <div id="container"></div>
    <script type="module">
      import * as THREE from 'three';
      import ThreeAMapBase from '../ThreeAMapBase.js';
      import { createAreaCanvas, loadGeojson } from './createAreaCanvas.js';
      class MyThreeAMap extends ThreeAMapBase {
        constructor() {
          super();
          this.center = [113.264499, 23.130061];
          this.zoom = 10;
          this.pitch = 68;
          this.adcode = 440100;
          this.mapConfig = {
            // showLabel: true,

            layers: [new AMap.TileLayer.Satellite()]
            // mapStyle: 'amap://styles/normal'
          };
        }
        createPlane(canvas, b) {
          //计算行政区范围的中心点
          const center = [(b.minlng + b.maxlng) * 0.5, (b.minlat + b.maxlat) * 0.5];

          //获取行政区范围对应的墨卡托投影坐标xyz
          const startp = this.customCoords.lngLatToCoord([b.minlng, b.minlat]);
          const endp = this.customCoords.lngLatToCoord([b.maxlng, b.maxlat]);
          //计算行政区范围的宽高
          const w = Math.abs(startp[0] - endp[0]);
          const h = Math.abs(startp[1] - endp[1]);
          //创建平面
          const geometry = new THREE.PlaneGeometry(w, h);
          const tex = new THREE.CanvasTexture(canvas);
          tex.minFilter = THREE.LinearMipmapNearestFilter;
          //将行政区卫星贴图作为材质贴图，开启透明
          const material = new THREE.MeshBasicMaterial({
            map: tex,
            transparent: true,
            depthWrite: false
          });
          const plane = new THREE.Mesh(geometry, material);
          //行政区中心点的墨卡托投影坐标xyz
          const p = this.customCoords.lngLatToCoord(center);
          //将平面坐标设置成行政区中心点墨卡托投影坐标
          plane.position.set(p[0], p[1], 0);
          return { plane, center, p, material };
        }
        async createChart() {
          const geojson = await loadGeojson(this.adcode);
          const { canvas, bound: b } = await createAreaCanvas(geojson, this.zoom);

          const { plane, center } = this.createPlane(canvas, b);
          //将地图定位到中心点
          this.map.setCenter(center);
          this.scene.add(plane);
        }
      }

      var myThreeMap = new MyThreeAMap();
      myThreeMap.init(document.getElementById('container'));
    </script>
  </body>
</html>
